﻿using System;
using System.Data;
using BO.Infrastructure.StaticData;
using BO.Infrastructure.Utils;
using BO.News;
using ChannelVN.Cached;
using GUI.Extensions;

namespace GUI.Controls.Sport
{
    public partial class HighlightNewsAtHomePage : System.Web.UI.UserControl
    {
        private const string LiFormat = "<li>{4}<h2 class=\"hidden\"><a href=\"{0}\" title=\"{2}\">{1}{5}</a></h2><p class=\"hidden\">{3}</p></li>";
        private const string TopItemFormat = "<li>{4}<h2><a href=\"{0}\" title=\"{2}\">{1}{5}</a></h2><p>{3}</p></li>";

        private const int ImageTopWidth = 430;
        private const int ImageTopHeight = 320;
        private const int Top = 5;

        public bool GetDataFromNoCacheLayer = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            var enableHtmlCached = GetDataFromNoCacheLayer && CacheSettingExtensions.EnableCacheHtml;

            var bo = enableHtmlCached ? NewsBO_NoCache.GetInstance() : NewsBO.GetInstance();
            var isCachedUpdating = enableHtmlCached && CacheController.IsCachedUpdating;

            var dt = bo.GetSportNewsByPosition(top: Top, typeId: DefinedConstants.SportNews.TypeIds.NoiBatTrangChu, getDataFromMasterDataBase: isCachedUpdating);
            Visible = DataBind(dt);
        }

        private bool DataBind(DataTable dt)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                {
                    var r = dt.Rows[0];
                    var avatar = PresentationUtils.Photo.BuildNewsAvatar(r, ImageTopWidth, ImageTopHeight);

                    this.ltrTopItem.Text = string.Format(
                                    TopItemFormat,
                                    r[SqlConstants.NewsPublish.Url],
                                    r[SqlConstants.NewsPublish.Title],
                                    r[SqlConstants.NewsPublish.EncodedTitle],
                                   r[SqlConstants.NewsPublish.InitSapo],
                                    avatar,
                                    r[SqlConstants.NewsPublish.NewsTypeIcon]);
                }
                var sb = new System.Text.StringBuilder();


                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var r = dt.Rows[i];
                    var avatar = PresentationUtils.Photo.BuildNewsAvatar(r, ImageTopWidth, ImageTopHeight);

                    sb.Append(string.Format(
                            LiFormat,
                            r[SqlConstants.NewsPublish.Url],
                            r[SqlConstants.NewsPublish.Title],
                            r[SqlConstants.NewsPublish.EncodedTitle],
                             r[SqlConstants.NewsPublish.InitSapo],
                            avatar,
                            r[SqlConstants.NewsPublish.NewsTypeIcon]));

                }
                ltrList.Text = sb.ToString();
                return true;
            }
            return false;
        }

    }
}